MASK1 = 4294967296  # 2^32
MASK2 = 2147483648  # 2^31
MASK3 = 2147483647  # 2^31-1


class Solution:
    def getSum(self, a: int, b: int) -> int:
        while b != 0:
            carry = ((a & b) << 1) % MASK1
            a = (a ^ b) % MASK1
            b = carry

        if a < MASK2:
            return a
        else:
            return ~((a % MASK2) ^ MASK3)


if __name__ == "__main__":
    print(Solution().getSum(1, 2))  # 3
    print(Solution().getSum(-2, 3))  # 1

    print(Solution().getSum(-8, -12))  # -20
    # print("a:", bin(a), "->", bin(a % MASK2), "->", bin((a % MASK2) ^ MASK3), "->", bin(~((a % MASK2) ^ MASK3)))
    # a: 0b11111111111111111111111111101100 -> 0b1111111111111111111111111101100 -> 0b10011 -> -0b10100
